package com.openbravo.pos.panels;

import javax.swing.JPanel;
import java.awt.BorderLayout;

import javax.swing.AbstractListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;


import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;

public class JStringRangeGeneratPanel extends JPanel {
	private JTextField txtPrefix;
	private JTextField txtSuffix;
	private JTextField txtNum;
	private JTextField txtInterval;
	private AbstractListModel model;
	private ArrayList listString;
	private JList list;

	/**
	 * Create the panel.
	 */
	public JStringRangeGeneratPanel() {
		initComponents();
		listString=new ArrayList();
	
	}
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(200, 10));
		add(panel, BorderLayout.EAST);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel(AppLocal.getIntString("label.txtPrefix"));
		lblNewLabel.setBounds(23, 10, 72, 15);
		panel.add(lblNewLabel);
		
		txtPrefix = new JTextField();
		txtPrefix.setBounds(94, 7, 93, 21);
		panel.add(txtPrefix);
		txtPrefix.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel(AppLocal.getIntString("label.txtSuffix"));
		lblNewLabel_1.setBounds(23, 34, 72, 15);
		panel.add(lblNewLabel_1);
		
		txtSuffix = new JTextField();
		txtSuffix.setHorizontalAlignment(SwingConstants.CENTER);
		txtSuffix.setBounds(94, 31, 93, 21);
		panel.add(txtSuffix);
		txtSuffix.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel(AppLocal.getIntString("label.txtNum"));
		lblNewLabel_2.setBounds(23, 65, 72, 15);
		panel.add(lblNewLabel_2);
		
		txtNum = new JTextField();
		txtNum.setHorizontalAlignment(SwingConstants.CENTER);
		txtNum.setBounds(94, 62, 93, 21);
		panel.add(txtNum);
		txtNum.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel(AppLocal.getIntString("label.txtInterval"));
		lblNewLabel_3.setBounds(23, 96, 72, 15);
		panel.add(lblNewLabel_3);
		
		txtInterval = new JTextField();
		txtInterval.setHorizontalAlignment(SwingConstants.CENTER);
		txtInterval.setText("1");
		txtInterval.setBounds(94, 93, 93, 21);
		panel.add(txtInterval);
		txtInterval.setColumns(10);
		
		JButton btnNewButton = new JButton(AppLocal.getIntString("label.generat"));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				generat();
			}
		});
		btnNewButton.setBounds(51, 130, 93, 23);
		panel.add(btnNewButton);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(10, 161, 177, 90);
		panel.add(textArea);
		textArea.setText(AppLocal.getIntString("label.txtPrefix")+"+"+AppLocal.getIntString("label.txtSuffix")+"\n"+AppLocal.getIntString("label.txtSuffix")+"="+AppLocal.getIntString("label.txtSuffix")+"+"+AppLocal.getIntString("label.txtInterval"));
		list = new JList();
	     JScrollPane scrollPane = new JScrollPane();
	    scrollPane.setViewportView(list);
	     add(scrollPane, BorderLayout.CENTER);
	
	}
	
	
	protected void generat() {
		String prefix=txtPrefix.getText();
		try {
			int suffix=(Integer) Formats.INT.parseValue(txtSuffix.getText());
			int num=(Integer) Formats.INT.parseValue(txtNum.getText());
			int interval=(Integer) Formats.INT.parseValue(txtInterval.getText());
			listString=new ArrayList();
			
			for(int i=0;i<num;i++){
				int lineNum=suffix+i*interval;
				listString.add(prefix+lineNum);
			}
			list.setModel(new AbstractListModel(){
	            @Override
	            public int getSize() {
	                return listString.size();
	            }

	            @Override
	            public Object getElementAt(int index) {
	                return listString.get(index);
	            }
	        });
		} catch (BasicException e) {
			new MessageInf(e).show(this);
		}
	}
	public ArrayList<String> getListString() {
		return listString;
	}
	
	public void reset(){
		this.listString=new ArrayList();
	}
}
